home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98a.txt / 000018_icon-group-sender _Fri Jan 23 12:33:53 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.8.7/8.8.7) with SMTP id MAA12604
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Fri, 23 Jan 1998 12:33:53 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA02606; Fri, 23 Jan 1998 12:33:53 -0700
  7. Date: Fri, 23 Jan 1998 10:42:53 -0700
  8. From: swampler@noao.edu (Steve Wampler)
  9. Subject: Re: Stripping blank and comment lines
  10. To: icon-group@optima.CS.Arizona.EDU
  11. Message-Id: <swampler-9800231742.AA007714605@orpheus.gemini.edu>
  12. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  13. Status: RO
  14. Content-Length: 3070
  15.  
  16.  
  17. A while ago I posed a problem to this group to write a program that
  18. strips comment lines (lines with only a comment on them) and
  19. blank lines from Icon source files.  Only Nevin Liber provided
  20. a solution (thanks Nevin!), which works just fine and is reproduced
  21. below.  I've also included the solution I had, which is similar
  22. to Nevin's but less sophisticated about error checking.  Finally,
  23. I've included a version that attempts to avoid the Wrath of the
  24. Variable Name gods - it's a rewrite of my solution without any
  25. local variables!  Now a new puzzle - can you rewrite the if test
  26. it that version's 'stripcomments()' procedure to avoid the the
  27. empty 'then' clause?
  28.  
  29. ----First, Nevin Liber's solution (I've scrunched some of the blank
  30. lines to save space) - note the clever way he has of keeping
  31. the file opening and closing isolated into a single procedure, while
  32. processing the file contents outside that procedure.  I had not
  33. seen this before!:
  34.  
  35. procedure main(LArguments)
  36.     local    fInput
  37.     local    sLine
  38.  
  39.     every fInput := InputFiles(LArguments) do {
  40.         while sLine := read(fInput) do {
  41.             write(ValidSourceLine(sLine))
  42.         }
  43.     }
  44. end
  45.  
  46. procedure InputFiles(LFilenames)
  47.     local    sFilename
  48.     local    fFile
  49.  
  50.     if 0 = *LFilenames then {
  51.         return &input
  52.     }
  53.  
  54.     every sFilename := !LFilenames do {
  55.         if not (fFile := open(sFilename)) then {
  56.             write(&errout, "Cannot open ", image(sFilename), " for reading.")
  57.             next
  58.         }
  59.         suspend fFile
  60.         close(fFile)
  61.     }
  62. end
  63.  
  64. procedure ValidSourceLine(sLine)
  65.  
  66.     sLine ? {
  67.         tab(many('\t '))
  68.         if ="#" | not move(1) then {
  69.             fail
  70.         }
  71.     }
  72.  
  73.     return sLine
  74. end
  75. -------------------------------------------------
  76. Now my solution:
  77. -------------------------------------------------
  78. # remove comment lines and blank lines from Icon programs...
  79. #   (one can argue that a blank line is just an exceptionally
  80. #    terse comment...)
  81.  
  82. procedure main(args)
  83.     local f
  84.  
  85.     if *args = 0 then {
  86.         stripcomments(&input)
  87.         }
  88.     else {
  89.         every f := open(!args) do {
  90.             stripcomments(f)
  91.             close(f)
  92.             }
  93.         }
  94.     
  95. end
  96.     
  97. procedure stripcomments(f)
  98.     local line
  99.  
  100.     every line := !f do {
  101.         line ? {
  102.            tab(many(' \t'))
  103.            if ="#" | pos(0) then next
  104.            }
  105.         write(line)
  106.         }
  107.  
  108. end
  109. ------------------------------------
  110. And, finally, the 'variable-less' version:
  111. ------------------------------------
  112. # remove comment lines and blank lines from Icon programs...
  113. #   (one can argue that a blank line is just an exceptionally
  114. #    terse comment...)
  115.  
  116. procedure main(args)
  117.  
  118.     if *args = 0 then {
  119.         stripcomments(&input)
  120.         }
  121.     else {
  122.         every close(stripcomments(open(!args)))
  123.         }
  124.     
  125. end
  126.     
  127. procedure stripcomments(f)
  128.  
  129.     every !f ? {
  130.            tab(many(' \t'))
  131.            if ="#" | pos(0) then {}    # can you remove this empty clause?
  132.            else write(&subject)
  133.            }
  134.  
  135.     return f
  136. end
  137.  
  138. --
  139. Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under AURA)]
  140. The gods that smiled at your birth are now laughing openly. (Fortune Cookie)
  141.